package cn.zd.service;

import cn.zd.dao.ForeignExchangeMapper;
import cn.zd.pojo.ForeignExchangeEntity;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Random;

/**
 * @author zhangdong
 * @version 1.0.0
 * <p>
 */
@Service
public class ForeignExchangeServiceImpl implements ForeignExchangeService {

    @Autowired
    ForeignExchangeMapper foreignExchangeMapper;

    private int indexId = 1;

    public void parsePage(Document document, int titleIndex, int index, String createAt) {

        System.out.println("抓取数据第" + index + "条开始...");

        String startSelector = "body > div.wrap.listpage.clearfix > div.pageleft.fl > div.content > p:nth-child(";

        ForeignExchangeEntity foreignExchange = new ForeignExchangeEntity();
        foreignExchange.setId(indexId);
        indexId++;

        Element title = document.selectFirst(startSelector + titleIndex + ")");
        if (Objects.isNull(title)) throw new RuntimeException("");
        foreignExchange.setTitle(title.text());

        Element startPrice = document.selectFirst(startSelector + index + ")");
        if (Objects.isNull(startPrice)) throw new RuntimeException("");
        foreignExchange.setStartPrice(startPrice.text());

        int i = (int) (1 + Math.random() * 10);
        Element minUnit = document.selectFirst(startSelector + (index + 1) + ")");
        if (Objects.isNull(minUnit)) throw new RuntimeException("");
        String _minUnit = minUnit.text();
        String _minUnit1 = _minUnit.substring(0, _minUnit.length() - 1) + i;
        String _minUnit2 = _minUnit.substring(_minUnit.length() - 1);
        foreignExchange.setMinUnit(_minUnit1 + _minUnit2);

        Element gangGan = document.selectFirst(startSelector + (index + 2) + ")");
        if (Objects.isNull(gangGan)) throw new RuntimeException("");
        String _gangGan = gangGan.text();
        String _gangGan1 = _gangGan.substring(0, _gangGan.length() - 1) + i;
        String _gangGan2 = _gangGan.substring(_gangGan.length() - 1);
        foreignExchange.setGangGan(_gangGan1 + _gangGan2);

        foreignExchange.setCreateAt(createAt);

        System.out.println(foreignExchange);
        foreignExchangeMapper.insert(foreignExchange);

        System.out.println("抓取数据结束" + index + "条结束...");

    }

    @Override
    public void init() {
        foreignExchangeMapper.deleteAll();
    }

    @Override
    public List<ForeignExchangeEntity> queryAll() {
        return foreignExchangeMapper.queryAll();
    }

    @Override public List<ForeignExchangeEntity> query4() {
        return foreignExchangeMapper.query4();
    }

    @Override public List<ForeignExchangeEntity> queryLikeTitle(String title) {
        return foreignExchangeMapper.queryLikeTitle(title);
    }

    public static void main(String[] args) {
        int i = (int) (1 + Math.random() * 10);
        String str = "123456";
        String substring = str.substring(0, str.length() - 1);
        String substring1 = str.substring(str.length() - 1);
        System.out.println();
    }
}
